PrintDocument PrintPreviewDialog and PrintDialog Control in C#.Net
Where, The PrintDialogControls are used to open the Windows Print Dialog. The PrintDocument component allows users to send an output to a printer. With the help of PrintPreviewDialog, You can preview a document.
How to PrintDocument and check PrintPreview
Drag and drop PrintDocument control, PrintDialog control and PrintPreviewDialog control from the toolbox on the WindowForm.
PrintDocument: The PrintDocument object encapsulates all the information needed to print a page. They associate with the control which content can be print. They handle the events and operations of printing.
Step1: Associate control to Print document
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawString(richTextBox1.Text, richTextBox1.Font, Brushes.Black, 100, 20);
e.Graphics.PageUnit = GraphicsUnit.Inch;
}
Step2: write code for printing and print preview.
private void btnPrint_Click(object sender, EventArgs e)
{
//PrintDialog associate with PrintDocument;
printDialog1.Document = printDocument1;
if (printDialog1.ShowDialog()==DialogResult.OK)
{
printDocument1.Print();
}
}
private void btnPrintPreview_Click(object sender, EventArgs e)
{
//Associate PrintPreviewDialog with PrintDocument.
printPreviewDialog1.Document = printDocument1;
// Show PrintPreview Dialog
printPreviewDialog1.ShowDialog();
}
Run the project
When you click PrintPreview Button then PrintPreview dialog will open.
When click Print Button then Print dialog will open.
When you click Print Button then output will goes to the Printer.
Ali Ahmed
24-Dec-2017how can i remove that gray space or bg in printpreview dialog and in printpreviewcontrol any advice please ?? i want only page to be visible not the bg ??
Manoj Bhatt
29-Sep-2011Thanks Puspendra,
Your article and discussion helped me alot.
Uttam Misra
29-Jul-2011Join MindStick Forum for technical discussion.
priya R
28-Jul-2011Anonymous User
27-Jul-2011You have used e.HasMorePages=true. Its working but problem is that you have to put it in loop because at the end of first page it becomes false. Here I will give an algorithm which might be solve your problem.
private void print_doc(......)
{
TextDocument doc = (TextDocument)sender;
//Some code is written here.
.
.
float x = e.MarginBounds.Left; //set left margin.
float y = e.MarginBounds.Top; //set right margin.
doc.PageNumber += 1; //increasing page number by one.
if (doc.Offset < doc.Text.GetUpperBound(0))
{
e.HasMorePages = true;
} else {
doc.Offset = 0;
}
}
You can modify this code according to your requirements and still if you get any problem then you can ask it with your sample code on mindstick forum or on here.
Thanks.
priya R
27-Jul-2011priya R
27-Jul-2011